<?php
/**
 * Created by PhpStorm.
 * User: mhx
 * Date: 17-4-7
 * Time: 上午9:17
 */
class gallerycomehereController extends baseController
{
    public function __construct()
    {
        parent::__construct();

    }

    /**
     *
     */
    public function run()
    {
        $url_perfix = 'http://gallery.artron.net/class/0-0-0-';
        $url_postfix = '.html';
        //$page = 1;
        $map = new mapController();
        $cityid = new commonController();

        for($i = 1;$i<499;$i++) {
            $url = $url_perfix . $i . $url_postfix;
            echo '画廊',$i;
            $contents = url_crawler($url);
            if ($contents != false) {
                $urllist = $this->getUrllist($contents);
                //print_r($urllist);die;
                $j = 0;

                while (isset($urllist[$j])) {
                    $waittosave = array();
                    $isupdate= 0;
                    //print_r($urllist[$j]);
                    $muyou = model('companies')->find('apellation="'.$urllist[$j]['name'].'"');
                    if($muyou){

                        if($muyou['website']==''&&$muyou['address']==''){
                            $isupdate =1;
                        }else{

                            ++$j;
                            continue;
                        }

                    }
//                     print_r($urllist[$j]);
//                    echo '<------>';
//                    echo $j;
                    //只抓取其中about页面的数据
                    $realurl = $this->getrealurl($urllist[$j]['url']);
                    //echo $realurl;die;
                    $contents = url_crawler($realurl);
                    $pos = mb_strpos($contents, '或许，点击这里');
                    if ($pos !== false && $contents == '') {
                        ++$j;
                        continue;
                    }
                    $waittosave = $this->getdetails($contents, $urllist[$j]);
                    if ($waittosave == false) {
                        ++$j;
                        continue;
                    }
//                    print_r($waittosave);die;

                    if ($waittosave['logourl'] != '') {
                        $image = $this->saveImage($waittosave['logourl']);
                        if ($image != false) {
                            $waittosave['logo'] = 'museum/' . $image;
                        } else {
                            $waittosave['logo'] = '';
                        }
                    } else {
                        $waittosave['logo'] = '';
                    }
                    if($waittosave['city']!='') {
                        $city = model('city')->find('id=' . $waittosave['city']);
                    }else{
                        $city = null;
                    }
                    $locatoin = $map->getlocation($waittosave['address'],$city);

                    if($locatoin == false){
                        $locatoin = $map->getlocation($waittosave['apellation'],$city);
                    }
                    if($locatoin == false){
                        $waittosave['longitude'] = 0;
                        $waittosave['latitude'] = 0;
                    }else{
                        $waittosave['longitude'] = $locatoin['longitude'];
                        $waittosave['latitude'] = $locatoin['latitude'];
                    }

                    if($waittosave['city'] == ''&&$locatoin['city']!=''){
                        $thecity = $cityid->getcityid($locatoin['city']);
                        $waittosave['city'] = $thecity['id'];
                    }
                    if($waittosave['city']==''){
                        $waittosave['city'] = 0;
                    }

                    //print_r($waittosave);die;
                    $waittosave['fax']='';
                    if($isupdate==0) {
                        model('companies')->insert($waittosave);
                    }else{
                        echo 'update!!!!!!!!';
                        print_r($waittosave);
                        model('companies')->update('apellation="'.$waittosave['apellation'].'"',$waittosave);
                    }

                    //die;
                    /*//存数据表里name,id;
                    $fix = model('mazhanxunfix')->select('id=1');
                    $fix[0]['artist'] = $fix['artist'] . $waittosave['name'] . ',' . $id . ';';
                    //print_r($fix[0]);die;
                    model('mazhanxunfix')->update('id=1', $fix[0]);
                    //die;
                    $waittosave = array();
//                    echo 'daol!!!!!!!!!!!!!!';*/

                    ++$j;
                }

            }
        }

        return null;
    }


    protected function getUrllist($contents)
    {
        $pos_img = mb_strpos($contents,'var arr=get_arr(');
        $imgurl_contents = mb_substr($contents,$pos_img);
        $pos_img = mb_strpos($imgurl_contents,';');
        $imgurl_contents = mb_substr($imgurl_contents,0,$pos_img);


        $pos = mb_strpos($contents,'class="shopList">');
        $urllist = array();
        $i = 0 ;
        while ($pos !== false){
            //获取imgurl
            $pos_img = mb_strpos($imgurl_contents,'http:');
            $imgurl_contents = mb_substr($imgurl_contents,$pos_img);
            $pos_img = mb_strpos($imgurl_contents,'"');
            $imgurl = mb_substr($imgurl_contents,0,$pos_img);
            if(!strcmp($imgurl,'http:\/\/img14.artimg.net\/gallery\/201011\/20101201B.jpg.tiny.jpg')){
                $urllist[$i]['imageurl'] = '';
            }else{
                $urllist[$i]['imageurl'] = $imgurl;
            }
            $imgurl_contents = mb_substr($imgurl_contents,$pos_img);

            //获取其他信息
            $contents = mb_substr($contents,$pos+mb_strlen('class="shopList">'));

            $pos = mb_strpos($contents,'href="');
            $contents = mb_substr($contents,$pos+mb_strlen('href="'));
            $pos = mb_strpos($contents,'"');
            $urllist[$i]['url'] = mb_substr($contents,0,$pos);
            $pos = mb_strpos($contents,'>');
            $contents = mb_substr($contents,$pos+mb_strlen('>'));

            $pos = mb_strpos($contents,'</a');
            $urllist[$i]['name'] =$this->killkongge(strip_tags(mb_substr($contents,0,$pos)));

            //$muyou = model('mamuseum')->find('name="'.$urllist[$i]['name'].'"');

            $pos = mb_strpos($contents,'经营年限：');
            $contents = mb_substr($contents,$pos+mb_strlen('经营年限：'));
            $pos = mb_strpos($contents,'</p');

            $age = intval( $this->killkongge(strip_tags(mb_substr($contents,0,$pos))));
            $today =intval(date('Y'));
            $year = $today - $age;
            $urllist[$i]['year'] = $year;

            $pos = mb_strpos($contents,'所在城市：');
            $contents = mb_substr($contents,$pos +mb_strlen('所在城市：'));
            $pos = mb_strpos($contents,'</b');
            $urllist[$i]['province'] = $this->killkongge(strip_tags(mb_substr($contents,0,$pos)));
            $contents = mb_substr($contents,$pos+mb_strlen('</b>'));
            $pos = mb_strpos($contents,'</b');
            $urllist[$i]['city'] = $this->killkongge(strip_tags(mb_substr($contents,0,$pos)));

            $pos = mb_strpos($contents,'主营项目：');
            $contents = mb_substr($contents,$pos+mb_strlen('主营项目：'));
            $pos = mb_strpos($contents,'</p');
            $urllist[$i]['project'] = strip_tags(mb_substr($contents,0,$pos));

            $pos = mb_strpos($contents,'class="shopList">');

            ++$i;
        }

        return $urllist;

    }
    //分析详情页
    protected function getdetails($contents,$olddata)
    {
        $ret = array();

        $pos = mb_strpos($contents,'class="details tabs mt20">');
        $contents = mb_substr($contents,$pos+mb_strlen('class="details tabs mt20">'));
        $pos = mb_strpos($contents,'language="javascript">');
        $contents = mb_substr($contents,0,$pos +mb_strlen('language="javascript">'));

        $pos = mb_strpos($contents,'p>');
        $contents = mb_substr($contents,$pos + mb_strlen('p>'));
        $pos = mb_strpos($contents,'</p');
        $ret['about'] = $this->keeponeline(strip_tags(mb_substr($contents,0,$pos)));
        $pos = mb_strpos($contents,'店主名称：');
        if($pos !==false){
            $contents = mb_substr($contents,$pos+mb_strlen('店主名称：'));
            $pos = mb_strpos($contents,'<');
            $ret['corporation'] = $this->killkongge(strip_tags(mb_substr($contents,0,$pos)));
        }else{
            $ret['corporation'] = '';
        }
        $pos = mb_strpos($contents,'地　　址：');
        if($pos !== false){
            $contents = mb_substr($contents,$pos+mb_strlen('地　　址：'));
            $pos = mb_strpos($contents,'</p');
            $ret['address'] =$this->killkongge(strip_tags(mb_substr($contents,0,$pos)));
        }else{
            $ret['address'] = '';
        }
        $pos = mb_strpos($contents,'电　　话：');
        if($pos !== false){
            $contents = mb_substr($contents,$pos+mb_strlen('电　　话：'));
            $pos = mb_strpos($contents,'</p');
            $ret['tel'] =$this->killkongge(strip_tags(mb_substr($contents,0,$pos)));
        }else{
            $ret['tel'] = '';
        }
        $pos = mb_strpos($contents,'座　　机：');
        if($pos !== false){
            $contents = mb_substr($contents,$pos+mb_strlen('座　　机：'));
            $pos = mb_strpos($contents,'</p');
            $ret['tel'] =$this->killkongge(strip_tags(mb_substr($contents,0,$pos)));
        }else{
            $ret['tel'] = '';
        }$pos = mb_strpos($contents,'邮　　箱：');
        if($pos !== false){
            $contents = mb_substr($contents,$pos+mb_strlen('邮　　箱：'));
            $pos = mb_strpos($contents,'</p');
            $ret['email'] =$this->killkongge(strip_tags(mb_substr($contents,0,$pos)));
        }else{
            $ret['email'] = '';
        }

        $ret['apellation'] = $olddata['name'];
        //$ret['year'] = $olddata['year'];
        //$ret['province'] = $olddata['province'];

        $common = new commonController();
        $city = $common->getcityid($olddata['city']);

        $ret['city'] = $city['id'];
        $ret['business'] = $olddata['project'];
        $ret['logourl'] = $olddata['imageurl'];
        $ret['website'] = $olddata['url'];


        return $ret;
    }
    //分析组装url
    protected function getrealurl($url){
        //$realurl = '';
        $num_arr = explode('/',$url);
        $i = 0;
        while (isset($num_arr[$i])){
            $i++;
        }
        $num = $num_arr[$i-1];
        $realurl = $url.'/g_infor'.$num.'.html';

        return $realurl;
    }

    protected function saveImage($img_url){

        /*echo 'This is save Img:::';
        echo $img_url;
        echo '<br />';*/

        $folder = ROOT_PATH.'museum/';

        if(!is_dir($folder)){
            mkdir($folder,0777);
            //chmod($folder,0777);
        }

        //echo 'url：',$img_url,'<br />';

        $pic_format = array('.bmp','.jpg','.tiff','.gif','.pcx','.tga','.exif','.jpeg','.png',
            '.fpx','.svg','.psd','.cdr','.pcd','.dxf','.ufo','.eps','.ai','.raw','.WMF');

        //echo 'saveImage:','<br />','imgurl:',$img_url,'<br />','path:',$folder,'<br />';
        $img_url = str_replace('\\','',$img_url);

        $end = explode(".", $img_url);
        $temp_i = 0;
        while(isset($end[$temp_i])){
            ++$temp_i;
        }
        $end = "." . $end[$temp_i-1];

        if(!in_array($end,$pic_format)){
            return false;
        }
        //echo 'url没问题','<br />';
        $ch = curl_init();
        //echo 'curl没问题','<br />';
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //要头信息
        curl_setopt($ch,CURLOPT_HEADER,1);
        //不要正文
        curl_setopt($ch, CURLOPT_NOBODY, 1);

        curl_setopt($ch,CURLOPT_URL,$img_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $contents = curl_exec($ch);
        //echo 'contents::',$contents,'<br />';

        /*$content_type = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);

        echo $content_type;die;*/
        curl_close($ch);

        //echo '$contents:::',$contents,'<br />';
        $pos = mb_stripos($contents,'200');
        if($pos === false){
            $pos = mb_strpos($contents,'OK');
            if($pos === false){
                return '';
            }
        }

        $pic_name = $this->utime() . rand(1111, 9999) . $end;

        //set_time_limit(8 * 60 * 60); //限制最大的执行时间
        $destination_folder = $folder; //文件下载保存目录
        $newfname = $destination_folder . $pic_name;//文件PATH

        //echo 'newfname:',$newfname,'<br />';

        $ch = curl_init();
        try{$fp = fopen($newfname,'wb');}catch(Exception $err){
            cpError::write($err->getMessage());
            return 0;}
        //echo 'fopen is ok!!';
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch,CURLOPT_URL,$img_url);
        curl_setopt($ch,CURLOPT_FILE,$fp);

        curl_exec($ch);
        curl_close($ch);
        fclose($fp);

        //生成缩略图
        Image::thumb($newfname,$destination_folder.'64_'.$pic_name,'','640','640',6);


        /*$image = new \Think\Image();
        //echo $newfname;
        try{$image->open($newfname);}catch (Exception $err){return 0;}
        echo 'Thinkphp open is right!!!';
        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
        //echo $destination_folder.'64_'.$pic_name;die;
        try{$image->thumb(640, 640)->save($destination_folder.'64_'.$pic_name);}catch (Exception $err){return 0;}
        //echo 'Thinkphp thumb is right!!','<br />';
        //die;*/

        return $pic_name;
    }

    protected function utime()
    {
        return time();
    }

    protected function killkongge($str){
        $search = array(" ","　","\n","\r","\t","&nbsp;");
        $replace = array("","","","","","");
        return str_replace($search, $replace, $str);
    }
    protected function keeponeline($str){
        //先删空格再把多个换行符替换成一个
        $str = preg_replace ( "/\s(?=\s)/","\\1", $str);

        $search = array(" ");
        $replace = array("");
        $str =  str_replace($search, $replace, $str);

        $search = array("\n","\r","\t");
        $replace = array(" "," "," ");
        $str =  str_replace($search, $replace, $str);

        $str = preg_replace('/\s+/','\n',$str);
        $str = str_replace('&nbsp;','',$str);
        return $str;
    }

    /*
         * 字符串删除第一个或最后一个指定字符串，目前用于删除不用的单位
         *$theend = null ,删除第一个匹配，否则删除最后一个匹配
         * $p要删除的字符串
         */
    protected function deleteunit($str,$unit,$theend = null){
        if($theend != null){
            $relpos =0;
            //$lastpos = 0;
            $pos = mb_strpos($str,$unit);
            $temp = $str;
            //echo $str,'<br />';
            //echo 'pos:::',$pos,'<br />';
            if($pos  === false){return $str;}
            while($pos !== false){
//                echo 'while::relpos::',$relpos,'<br />';
//                echo 'while::pos::',$pos,'<br />';

                $relpos = $relpos+$pos+mb_strlen($unit);
                //$lastpos = $pos;
                $temp = mb_substr($temp,$pos+mb_strlen($unit));

//                echo 'temp:::',$temp,'<br />';

                $pos = mb_strpos($temp,$unit);

            }
//            echo 'out::relpos::',$relpos,'<br />';
//            echo 'out::lastpos::',$lastpos,'<br />';
            $relpos -= mb_strlen($unit);
            /*if($relpos != $lastpos){
                $relpos = $relpos + $lastpos;
            }*/

            //echo 'out::relpos::',$relpos,'<br />';
            //echo 'length::unit::',$unit,'<br />';
            $qian = mb_substr($str,0,$relpos);
            //echo 'qian::',$qian,'<br />';
            $hou = mb_substr($str,$relpos+mb_strlen($unit));
            //echo 'hou::',$hou,'<br />';
            $ret = $qian.$hou;

            return $ret;

        }else{
            $pos = mb_strpos($str,$unit);
            if($pos  === false){return $str;}
            $qian = mb_substr($str,0,$pos);
            $hou = mb_substr($str,$pos+mb_strlen($unit));
            $ret = $qian.$hou;
            return $ret;
        }
    }



}